package com.pank;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
 * ATM的入口类
 */

public class ATMSystem {
    public static void main(String[] args) {
        //定义一个用于存储账户的集合容器
        Scanner scanner=new Scanner(System.in);
        ArrayList<Account> accounts=new ArrayList<>();
        while (true) {
            System.out.println("================欢迎来到ATM系统===================");
            System.out.println("1、用户登录");
            System.out.println("2、用户注册");
            System.out.println("请输入你的操作:");
            int command = scanner.nextInt();
            switch (command){
                case 1:
                    //用户登录
                    login(accounts,scanner);
                    break;
                case 2:
                    //用户注册
                    register(accounts,scanner);
                    break;
                default:
                    System.out.println("你输入的操作不存在！");
            }


        }
    }

    /**
     * 用户登录功能
     * @param accounts
     */
    private static void login(ArrayList<Account> accounts,Scanner sc) {
        System.out.println("==========ATM银行系统登录界面===========");
        System.out.println("请输入你的卡号:");
        String cardId = sc.next();
        Boolean aBoolean = exitIdCard(cardId, accounts);
        if (aBoolean){
            System.out.println("请输入你的密码:");
            String password = sc.next();
            Boolean aBoolean1 = judgePassword(accounts, password, cardId);
            System.out.println(aBoolean1);
            if (aBoolean1){
                String userName = getUserName(accounts, cardId);
                System.out.println(userName+",欢迎您进入系统，您的卡号是:"+cardId);
                showUserCommand(sc,accounts,cardId);
            }
            else{
                System.out.println("您输入的密码不正确，请确认。");
            }
        }
        else {
            System.out.println("卡号不存在，请重新输入");
        }



    }

    /**
     * 展示用户登录功能页
     */
    public static void showUserCommand(Scanner scanner,ArrayList<Account> accounts,String cardId){
        while (true) {
            boolean flag=false;
            System.out.println("==============欢迎您进入ATM系统的用户操作界面===============");
            System.out.println("1、查询");
            System.out.println("2、存款");
            System.out.println("3、取款");
            System.out.println("4、转账");
            System.out.println("5、修改密码");
            System.out.println("6、退出");
            System.out.println("7、注销当前账户");
            System.out.println("请选择你要进行的功能操作:");
            int loginNum = scanner.nextInt();

            switch (loginNum){
                case 1:
                    queryUserInfor(cardId, accounts);
                    //查询
                    break;
                case 2:
                    //存款
                    saveMoney(cardId, accounts,scanner);
                    break;
                case 3:
                    //取款
                    drawMoney(cardId, accounts,scanner);
                    break;
                case 4:
                    //转账
                    transMoney(cardId,accounts,scanner);
                    break;
                case 5:
                    //修改密码
                    alterPassword(cardId,accounts,scanner);
                    break;
                case 6:
                    //退出登录
                    flag=exitSystem(flag);
                    break;
                case 7:

                    //注销当前用户
                    deleteAccount(cardId,accounts);
                    flag=true;
                    break;
                default:
                    System.out.println("你输入的指指令数字有误！");
            }
            if (flag){
                break;
            }
        }
    }

    /**
     * 注销用户
     * @param cardId
     * @param accounts
     */
    private static void deleteAccount(String cardId, ArrayList<Account> accounts) {
        Account accountByid = getAccountByid(cardId, accounts);
        accounts.remove(accountByid);
        System.out.println("注销成功！");
    }

    /**
     * 退出登录
     */
    public static Boolean exitSystem(Boolean flag) {
        flag=true;
        return flag;

    }

    /**
     * 修改密码
     * @param cardId 用户账号
     * @param accounts  账号集合
     * @param scanner
     */
    private static void alterPassword(String cardId, ArrayList<Account> accounts, Scanner scanner) {
        Account accountByid = getAccountByid(cardId, accounts);
        String oldPassward = accountByid.getCardPassward();
        while (true) {
            Boolean flag=true;
            System.out.println("请输入原密码:");
            String password = scanner.next();
            if (password.equals(oldPassward)){
                while (true) {
                    Boolean flag1=true;
                    System.out.println("请输入新密码:");
                    String newPassword = scanner.next();
                    System.out.println("请再次输入新密码:");
                    String secondPassword = scanner.next();
                    if (newPassword.equals(secondPassword)){
                        accountByid.setCardPassward(newPassword);
                        System.out.println("密码修改成功");
                    }else{
                        System.out.println("两次输入的密码不一样，请从新输入");
                        flag1=false;

                    }
                    if (flag1){
                        break;
                    }
                }
            }else{
                System.out.println("输入的原密码不正确,请重新输入");
                flag=false;
            }
            if (flag){
                break;
            }
        }

    }

    /**
     * 转账
     * @param cardId  用户账号
     * @param accounts  账号集合
     * @param scanner
     */
    public static void transMoney(String cardId, ArrayList<Account> accounts, Scanner scanner) {
        System.out.println("请输入转账的账号:");
        String anotherCard = scanner.next();
        Account accountByid1 = getAccountByid(anotherCard, accounts);//另一个账号的信息
        Boolean aBoolean = exitIdCard(anotherCard, accounts);
        Account accountByid = getAccountByid(cardId, accounts);//转账账号的信息
        double Nowmoney = accountByid.getMoney();
        double quotaMoney = accountByid.getQuotaMoney();
        double anotherMoney = accountByid1.getMoney();
        if (aBoolean){

            while (true) {
                boolean flag=true;
                System.out.println("请输入转账的金额:");
                int transMoney = scanner.nextInt();
                if (transMoney<=Nowmoney){
                    if (transMoney<=quotaMoney){
                        anotherMoney+=transMoney;
                        accountByid1.setMoney(anotherMoney);
                        Nowmoney-=transMoney;
                        accountByid.setMoney(Nowmoney);

                    }else{
                        System.out.println("转账金额超过了限定额度，请重新输入");
                        flag=false;
                    }


                }else{
                    System.out.println("自己都没钱，就别转了，请重新输入");
                    flag=false;
                }
                if (flag){
                    break;
                }
            }
        }
        System.out.println("转账成功!");
    }

    /**
     * 取款
     * @param cardId  用户账号
     * @param accounts  账号集合
     */
    public static void drawMoney(String cardId, ArrayList<Account> accounts,Scanner scanner) {
        System.out.println("请输入取款的金额:");
        int outMoney = scanner.nextInt();
        Account account = getAccountByid(cardId, accounts);
        double money = account.getMoney();
        money=money-outMoney;
        account.setMoney(money);
        queryUserInfor(cardId,accounts);
    }

    /**
     * 存款
     * @param cardId  用户账号
     * @param accounts   账号集合
     */
    private static void saveMoney(String cardId, ArrayList<Account> accounts,Scanner scanner) {
        System.out.println("请输入要存入的金额:");
        int inMoney = scanner.nextInt();
        Account account = getAccountByid(cardId, accounts);
        double money = account.getMoney();
        money=money+inMoney;
        account.setMoney(money);
        queryUserInfor(cardId,accounts);
    }

    /**
     * 查询用户信息
     * @param cardId 用户账号
     * @param accounts  账号集合
     */
    public static void queryUserInfor(String cardId, ArrayList<Account> accounts) {
        Account account = getAccountByid(cardId, accounts);
        System.out.println("用户名:"+account.getUserName());
        System.out.println("卡号:"+account.getCardId());
        System.out.println("账户密码:"+account.getCardPassward());
        System.out.println("卡上余额:"+account.getMoney());
        System.out.println("次取额度:"+account.getQuotaMoney());


    }

    /**
     * 根据id得到对象
     * @param cardId
     * @param accounts
     */
    public static Account getAccountByid(String cardId, ArrayList<Account> accounts) {
        for (int i = 0; i < accounts.size(); i++) {
            String cardId1 = accounts.get(i).getCardId();
            if (cardId1.equals(cardId)){
                return accounts.get(i);
            }

        }
        return null;
    }

    /**
     * 查询用户信息
     */


    /**
     * 获取某个id的用户名
     */
    public static String getUserName(ArrayList<Account> accounts,String cardId){
        for (int i = 0; i < accounts.size(); i++) {
            String Id = accounts.get(i).getCardId();
            if(Id.equals(cardId)){
                String userName = accounts.get(i).getUserName();
                return userName;
            }
        }
        return null;
    }


    /**
     * 判断密码是否正确
     */
    public static Boolean judgePassword(ArrayList<Account> accounts,String password,String cardId){
        for (int i = 0; i < accounts.size(); i++) {
            String Id = accounts.get(i).getCardId();
            if(Id.equals(cardId)){
                String cardPassward = accounts.get(i).getCardPassward();
                if (cardPassward.equals(password)){
                    return true;
                }else{
                    return false;
                }
            }
        }
      return null;
    }


    /**
     * 判断卡号是否存在
     */
    public static Boolean exitIdCard(String id,ArrayList<Account> accounts){
        for (int i = 0; i < accounts.size(); i++) {
            String cardId = accounts.get(i).getCardId();
            if (cardId.equals(id)){
                return true;
            }
        }
        return false;
    }

    /**
     * 用户开户功能的实现
     * @param accounts  接收的账户集合
     */
    private static void register(ArrayList<Account> accounts,Scanner sc ) {
        System.out.println("===============开户界面================");
        //1.创建account对象
        Account account=new Account();
        //2.录入账户信息，封装到对象中去

        System.out.println("请您输入账户名:");
        String name = sc.next();
        account.setUserName(name);

        while (true) {
            System.out.println("请您输入密码:");
            String password = sc.next();
            System.out.println("请您再次输入密码:");
            String passwordtwo = sc.next();
            if (passwordtwo.equals(password)){
                System.out.println("密码设置成功");
                account.setCardPassward(passwordtwo);
                break;//两次密码一样就跳出死循环了
            }else{
                System.out.println("密码不一致，请重新");
            }
        }
        //设置次交易最大限额
        System.out.println("请您输入账户最大限额");
        double quotaMoney = sc.nextDouble();
        account.setQuotaMoney(quotaMoney);

        //随机八位数当做卡号,且不重复
        String eightNumId = getEightNumId(accounts);
        account.setCardId(eightNumId);

        //3.将对象信息装到对象集合中去
        accounts.add(account);
        System.out.println("注册成功，你的卡号是:"+eightNumId);
    }

    /**
     * 获得八位随机数字当做卡号
     * @return  返回一个八位数字字符串
     */
    public static String getEightNumId(ArrayList<Account> accounts){
        String cardNum="";
        while (true) {
            Boolean flag=true;
            Random random=new Random();

            //循环八次得到卡号
            for (int i = 0; i <8; i++) {
                int i1 = (random.nextInt(10));
                cardNum+=i1;
            }
            System.out.println(cardNum);
            //使用死循环判断卡号是否重复，while经常会与flag一起使用，当flag==true是就退出死循环
            for (int i = 0; i < accounts.size(); i++) {
                String cardId = accounts.get(i).getCardId();
                System.out.println(cardNum);
                if (cardId.equals(cardNum)){
                    flag=false;
                    break;
                }
            }
            if(flag){
                break;
            }
        }
    return cardNum;//要是得不到不重复的卡号就执行这一行，一般是不会执行这一行的
    }
}
